import Vue from 'vue'

function initPlayerData() {
	return new Promise((resolve) => {
		Vue.prototype.$api.getOne({ top: 1 }).then(res => {
			const {
				data: {
					data
				}
			} = res
			if (data) {
				data.encryCardNum = Vue.prototype.$Utils.formatCardNum(data.cardNumber);
				data.formatBalance = uni.$u.priceFormat(data.balance, 2);
				Vue.prototype.$store.dispatch('set_userinfo', data)
			}
		}).catch(({
			isConnected
		}) => {
			if (isConnected) {
				Vue.prototype.$store.dispatch('set_userinfo', '')
			}
		}).finally(() => {
			resolve()
		})
	}).catch((e) => {

	});
}

function initData() {
	return new Promise((resolve) => {
		let promiseList = []
		// 初始化玩家数据
		promiseList.push(initPlayerData())
		Promise.all(promiseList).finally(() => {
			resolve()
		})
	}).catch((e) => {
		console.debug(e);
	});
}


module.exports = {
	initData,
	initPlayerData
}
